<route>
    {
        meta: {
            layout: 'index',
            title:'订单管理'
        }
    }  
  </route>
<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="订单名称" prop="orderName">
                <el-input v-model="queryParams.orderName" placeholder="请输入订单名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="订单类型" prop="orderType">
                <el-select v-model="queryParams.orderType" placeholder="订单类型" clearable>
                    <el-option v-for="dict in order_type_dict" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="分类状态" clearable>
                    <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
                <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain :icon="Plus" @click="handleAdd"
                    v-hasPermi="['grad:order:create']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain :icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['grad:order:delete']">删除</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="订单编号" align="center" width="200" prop="id" />
            <el-table-column label="订单名称" align="center" prop="orderName" />
            <el-table-column label="订单类型" align="center" prop="orderType" />
            <el-table-column label="客户" align="center" prop="customerName" />
            <el-table-column label="完成人" align="center" prop="producerName" />
            <!-- <el-table-column label="薪酬结算" align="center" prop="producerReward" /> -->
            <el-table-column label="订单总额" align="center" prop="amount" />
            <el-table-column label="已付金额" align="center" prop="receivedAmount">
                <!-- <template #default="scope">
                    {{ scope.row.receivedAmount }}
                    <el-button type="text" :icon="Edit" @click="editReceivedAmount(scope.row)"
                        v-hasPermi="['grad:order:update']"></el-button>
                </template> -->
            </el-table-column>
            <el-table-column label="备注" :show-overflow-tooltip="true" align="center" prop="remark" />
            <el-table-column label="状态" align="center" prop="status">
                <template #default="scope">
                    <el-switch :disabled="!isAdmin" v-model="scope.row.status" :active-value="1" :inactive-value="0"
                        @change="handleStatusChange(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <!-- <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createdTime) }}</span>
          </template>
        </el-table-column> -->
            <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button type="text" icon="View" @click="viewProcess(scope.row)">查看进度</el-button>
                    <!-- <el-button type="text" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['grad:order:delete']">删除</el-button>
  
            <el-button type="text" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['grad:order:update']">结算</el-button> -->
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />


        <!-- 添加或修改分类对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="订单名称" prop="orderName">
                    <el-input v-model="form.orderName" placeholder="请输入订单名称" style="width: 240px" />
                </el-form-item>
                <el-form-item label="订单类型" prop="orderType">
                    <el-select v-model="form.orderType" placeholder="订单类型" clearable>
                        <el-option v-for="dict in order_type_dict" :key="dict.value" :label="dict.label"
                            :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="客户" prop="customerId">
                    <el-tag type="info" v-if="form.customerId" size="large" style="margin: 0 1rem 0 0" :closable="true"
                        @close="form.customerId = 0">
                        {{ customer.nickName }}
                    </el-tag>
                    <!-- 分类选项 -->
                    <view v-else>
                        <el-button type="success" :icon="Plus" @click="customerSelect">选择</el-button>
                    </view>
                </el-form-item>

                <el-form-item label="完成人" prop="customerId">
                    <el-tag type="info" v-if="form.producerId" size="large" style="margin: 0 1rem 0 0" :closable="true"
                        @close="form.producerId = 0">
                        {{ producer.nickName }}
                    </el-tag>
                    <!-- 分类选项 -->
                    <view v-else>
                        <el-button type="success" :icon="Plus" @click="producerSelect">选择</el-button>
                    </view>
                </el-form-item>
                <el-form-item label="订单总额" prop="amount">
                    <el-input-number v-model="form.amount" controls-position="right" :min="0" />
                </el-form-item>
                <el-form-item label="已付金额" prop="receivedAmount">
                    <el-input-number v-model="form.receivedAmount" controls-position="right" :min="0" />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
                </el-form-item>
            </el-form>

            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
        <number-edit ref="numRef"  @confirm="editConfirm"></number-edit>
        <user-select ref="customerRef" v-model:userId="form.customerId" @confirm="customerConfirm"></user-select>
        <user-select ref="producerRef" v-model:userId="form.producerId" @confirm="producerConfirm"></user-select>

        <!-- <process-view :order="selectedOrder" ref="processRef"></process-view> -->
    </div>
</template>
<script setup>
import NumberEdit from '@/custom/pms/components/NumberEdit/index.vue';
import UserSelect from "@/custom/pms/components/UserSelect/index.vue";
import ProcessView from '@/custom/pms/components/ProcessView/index.vue';
import { queryList, createOne, deleteOne, queryOne, updateOne } from "@/custom/pms/api/order";
import { Delete, VideoPlay, Edit, Plus, Search, Refresh, Unlock, Lock, Files } from '@element-plus/icons-vue'
import userStore from "@/store/modules/user";
const isAdmin = ref(userStore().isAdmin);
import useApp from '@/hooks';
const { $, proxy } = useApp();
const selectedOrder = ref({});
const sys_normal_disable = ref([
    {
        value: 1,
        label: "正常",
    },
    {
        value: 0,
        label: "关闭",
    },
]);

const order_type_dict = ref([
    {
        value: "需求定制",
        label: "需求定制",
    },
    {
        value: "拓展开发",
        label: "拓展开发",
    },
]);
const tableData = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const customer = ref({});
const producer = ref({});

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        postCode: undefined,
        postName: undefined,
        status: undefined,
    },
    rules: {
        orderName: [{ required: true, message: "订单名称不能为空", trigger: "blur" }],
        orderType: [{ required: true, message: "订单类型不能为空", trigger: "blur" }],
    },
});

const { queryParams, form, rules } = toRefs(data);
// 点击事件
function viewProcess(row) {
    console.log(row.id)
    // selectedOrder.value = row;
    // proxy.$refs['processRef'].show();
    // proxy.$refs['processRef'].processQuery(row.id);
    $.navigateTo({
        url:'/pms/order/' + row.id
    })
}
// 修改确认
function editConfirm(e){
    console.log(e)
    form.value.receivedAmount = e;
    submitForm();
}
// 数字修改
function editReceivedAmount(item) {
    reset();
    //proxy.resetForm("formRef");
    form.value = item;
    proxy.$refs["numRef"].show();
}
// 客户选择
function customerSelect() {
    proxy.$refs["customerRef"].show();
}
function customerConfirm(res) {
    console.log(res);
    customer.value = res;
}
// 写手选择
function producerSelect() {
    proxy.$refs["producerRef"].show();
}
function producerConfirm(res) {
    producer.value = res;
}
/** 查询分类列表 */
function getList() {
    loading.value = true;
    queryList(queryParams.value).then((response) => {
        tableData.value = response.content;
        total.value = response.totalElements;
        loading.value = false;
    });
}
/** 取消按钮 */
function cancel() {
    open.value = false;
    reset();
}
/** 表单重置 */
function reset() {
    form.value = {
        orderName: undefined,
        orderType: undefined,
        customerId: undefined,
        producerId: undefined,
        amount: 0,
        receivedAmount: 0,
        remark: undefined,
    };
    proxy.resetForm("formRef");
}
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.id);

    single.value = selection.length != 1;
    multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
    // proxy.$refs["customerRef"].show();
    reset();
    open.value = true;
    title.value = "订单创建";
}
/** 提交按钮 */
function submitForm() {
    if (form.value.id != undefined) {
        updateOne(form.value).then((response) => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
        });
    } else {
        createOne(form.value).then((response) => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
        });
    }
}
/** 删除按钮操作 */
function handleDelete(row) {
    // fix 删除 undefined 错误
    const deleteIds = row.id ? [row.id] : ids.value;
    console.log(deleteIds);
    proxy.$modal
        .confirm('是否确认删除编号为"' + deleteIds + '"的订单？')
        .then(function () {
            let task = [];
            deleteIds.forEach(function (ele, index) {
                task.push(deleteOne(ele));
            });
            return new Promise(function (resolve, reject) {
                Promise.all(task).then((res) => {
                    resolve(res);
                });
            });
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
}
// 修改状态
function handleStatusChange(row) {
    const data = row;
    let text = row.status === 0 ? "关闭" : "恢复";
    proxy.$modal
        .confirm(`确认要 ${text} ${row.orderName} 吗?`)
        .then(function () {
            return updateOne(data);
        })
        .then(() => {
            proxy.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
            row.status = row.status === 0 ? 1 : 0;
        });
}
getList();
</script>
  